package com.corpgovernment.common.apollo;

import com.ctrip.corp.obt.generic.utils.Conditional;
import com.ctrip.corp.obt.generic.utils.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Configuration settings for the common.
 */
public interface CommonConfig {
    Set<String> DEFAULT_ERROR_CODE_PATHS = new HashSet<>(Arrays.asList("resultCode", "errno", "errorCode", "errCode", "status.errCode", "status.errorCode", "status.resultCode", "status.errno"));
    Set<String> DEFAULT_ERROR_MESSAGE_PATHS = new HashSet<>(Arrays.asList("errmsg", "errorMessage", "message", "status.errmsg", "status.errorMessage", "status.message"));

    /**
     * Get the value associated with a key.
     *
     * @param k Key to lookup in the config.
     * @return Value for the key or null if no key is present.
     */
    String get(String k);


    /**
     * 是否启用SupplierClient来进行分销接口的调用
     *
     * @return
     */
    default Boolean enabledSupplierClient() {
        String v = get("supplier.client.enabled");
        return Conditional.ofNullable(v).map(Boolean::valueOf).orElse(true);
    }

    /**
     * 分销错误码定位的路径
     *
     * @return
     */
    default Set<String> supplierErrorCodePaths() {
        String v = get("supplier.error.code.paths");
        if (StringUtils.isBlank(v)) {
            return DEFAULT_ERROR_CODE_PATHS;
        }
        Set<String> extralPaths = new HashSet<>(Arrays.asList(v.split("\\s*,\\s*")));
        extralPaths.addAll(DEFAULT_ERROR_CODE_PATHS);
        return extralPaths;
    }

    /**
     * 分销错误信息定位的路径
     *
     * @return
     */
    default Set<String> supplierErrorMessagePaths() {
        String v = get("supplier.error.message.paths");
        if (StringUtils.isBlank(v)) {
            return DEFAULT_ERROR_MESSAGE_PATHS;
        }
        Set<String> extralPaths = new HashSet<>(Arrays.asList(v.split("\\s*,\\s*")));
        extralPaths.addAll(DEFAULT_ERROR_MESSAGE_PATHS);
        return extralPaths;
    }



    /**
     * 获取需要脱敏的key
     *
     * @return
     */
    default Set<String> desensitizedKeys() {
        String v = get("desensitized.keys");
        if (StringUtils.isBlank(v)) {
            return new HashSet<>();
        }
        return new HashSet<>(Arrays.asList(v.split("\\s*,\\s*")));
    }

    /**
     * 获取需要脱敏的api
     *
     * @return
     */
    default Set<String> desensitizedApis() {
        String v = get("desensitized.apis");
        if (StringUtils.isBlank(v)) {
            return new HashSet<>();
        }
        return new HashSet<>(Arrays.asList(v.split("\\s*,\\s*")));
    }


}
